﻿using DL.Characters.Player.FSM.StateMachine;
using UnityEngine.InputSystem;

namespace DL.Characters.Player.FSM.State
{
    public class PlayerAirFallingState : PlayerOnAirState
    {
        //这个状态比较特殊，进入的方式有点特别
        //跳跃或者下坠失足
        //可进入的状态为 PlayerReJumpState  PlayerFastJumpDownState PlayerJumpDownState
        public PlayerAirFallingState(PlayerMoveStateMachine playerStateMachine, int property = 0) : base(playerStateMachine, property)
        {
        }

        public override void Enter()
        {
            base.Enter();
            ReusableData.InputMult = ReusableData.JumpScalableForwardSpeed;
            Animator.CrossFade("Falling",ReusableData.AniTransitionTime);
        }

        protected override void AddInputActionCallBacks()
        {
            base.AddInputActionCallBacks();
            StateMachine.InputSystem._playerInputActions.Space.started += OnEnterReJumpState;
        }

        protected override void RemoveInputActionCallBacks()
        {
            base.RemoveInputActionCallBacks();
            StateMachine.InputSystem._playerInputActions.Space.started -= OnEnterReJumpState;
        }

        private void OnEnterReJumpState(InputAction.CallbackContext context)
        {
            if (CanJump())
            {
                StateMachine.ChangeState<PlayerReJumpState>();
            }
        }

        private bool CanJump()
        {
            return ReusableData.IsJump && ReusableData.CurrentJumpCount < ReusableData.MaxJumpCount && !MoveController.CharacterIsOnGround;
        }
    }
}